<?php
require(__META_CONTROLS_GEN__ . '/MeetingMetaControlGen.class.php');


class MeetingMetaControl extends MeetingMetaControlGen {
    // Initialize fields with default values from database definition
    /*
		public function __construct($objParentObject, Meeting $objMeeting) {
			parent::__construct($objParentObject,$objMeeting);
			if ( !$this->blnEditMode ){
				$this->objMeeting->Initialize();
			}
		}
    */
    protected $dttTime;
    
    public function calDate_Create($strControlId = null) {
        $this->calDate = new QCalendarPopup($this->objParentObject, $strControlId);
        $this->calDate->Name = QApplication::Translate('Date');
        if ($this->objMeeting->Date)
            $this->calDate->DateTime = $this->objMeeting->Date;
        else{
            $DefualtDate = new QDateTime();
            $DefualtDate->SetDate(date("Y"), date("m"), date("d"));
            $this->calDate->DateTime= $DefualtDate;
        }

        return $this->calDate;
    }


    public function lstPeopleAsMembers_Create($strControlId = null) {

        $this->lstPeopleAsMembers = new AutoCompleteList($this->objParentObject, $strControlId);

        $this->lstPeopleAsMembers->Name = QApplication::Translate('People Attended');
        $objAssociatedArray = $this->objMeeting->GetPersonAsMembersArray();
        //$objPersonArray = Person::LoadArrayByOrgId($_SESSION['OrgId']);
        $objPersonArray = Person::LoadAll();
        if ($objPersonArray) foreach ($objPersonArray as $objPerson) {
                foreach ($objAssociatedArray as $objAssociated) {
                    if ($objAssociated->Id == $objPerson->Id)
                        $this->lstPeopleAsMembers->lst->AddItem($objPerson->FirstName. " "  .$objPerson->LastName  ,$objPerson->Id);
                }

            }
        $this->lstPeopleAsMembers->strCallList = "PersonOrg_Change";
        return $this->lstPeopleAsMembers;

    }

    protected function lstPeopleAsMembers_Update() {

        if ($this->lstPeopleAsMembers) {
            $this->objMeeting->UnassociateAllPeopleAsMembers();
            $imax = $this->lstPeopleAsMembers->lst->__get("ItemCount");

            for ($i=0; $i <$imax; $i++) {
                $objListItem = $this->lstPeopleAsMembers->lst->GetItem($i);;
                $this->objMeeting->AssociatePersonAsMembers(Person::Load($objListItem->Value));
            }
        }
    }



    public function SaveMeeting() {

        try {


            $this->dttTime = new QDateTime();
            $this->dttTime->setDate (date("Y"), date("m"), date("d"));
            $this->dttTime->setTime (date("H"), date("i"), date("s"));

            $CurrentUser = $_SESSION['User']['Id'];
            //should change user
            $Euser       = $_SESSION['User']['Id'];


            $this->objMeeting->Euser = $Euser;

            
            // Update any fields for controls that have been created
            if ($this->calDate) $this->objMeeting->Date = $this->calDate->DateTime;
            if ($this->txtLocation) $this->objMeeting->Location = $this->txtLocation->Text;
            if ($this->txtSubject) $this->objMeeting->Subject = $this->txtSubject->Text;


            if ($this->blnEditMode) {
                //Updating
                $this->objMeeting->UpdateBy   = $CurrentUser;
                $this->objMeeting->UpdateDate = $this->dttTime;
            }
            else {
                //New
                $this->objMeeting->CreateBy   = $CurrentUser;
                $this->objMeeting->CreateDate = $this->dttTime;
            }

       
            // Update any UniqueReverseReferences (if any) for controls that have been created for it

            // Save the Meeting object
            $this->objMeeting->Save();

            // Finally, update any ManyToManyReferences (if any)
            $this->lstPeopleAsMembers_Update();
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
    }

}

?>